package com.wxsong.authplatform.authweb.service.userrole;

import com.wxsong.authplatform.authweb.dao.userrole.UserRoleDao;
import com.wxsong.authplatform.authweb.model.userrole.UserRole;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserRoleServiceImpl implements UserRoleService {

    @Resource
    private UserRoleDao userRoleDao;

    @Override
    public List<UserRole> getByUserId(Long userId) {
        return userRoleDao.getByUserId(userId);
    }

    @Override
    public UserRole getById(Long id) {
        return userRoleDao.getById(id);
    }

    @Override
    public int add(UserRole userRole) {
        return userRoleDao.add(userRole);
    }

    @Override
    public int updateInfo(Long id, Long userId, Long roleId) {
        UserRole userRole = new UserRole(id, userId, roleId);
        return userRoleDao.update(userRole);
    }

    @Override
    public int delete(Long id) {
        return userRoleDao.delete(id);
    }

    @Override
    public int deleteByUserId(Long userId) {
        return userRoleDao.deleteByUserId(userId);
    }

    @Override
    public int modifyRoles(List<UserRole> userRoles) {
        userRoles.stream().forEach(userRole -> {
            userRoleDao.update(userRole);
        });
        return userRoles.size();
    }
}
